/**
 * 
 */
package leetCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.PriorityQueue;

/**
 * @author zhongfang
 *
 */
public class CourseScheduleIII {
    public int scheduleCourse(int[][] courses) {
    	Arrays.sort(courses,(a,b)->a[1]-b[1]);
    	int time=0;
    	int count=0;
    	int i=0;
    	for (int[] is : courses) {
    		if(time+is[0]<=is[1]){
    			count++;
    			time+=is[0];
    		}else {
    			int j = i-1;
				for (; j >=0 ; j--) {
					if(courses[j][0]>courses[i][0]){
						break;
					}
				}
				if(j!=-1){
					time+=courses[i][0]-courses[j][0];
				}
			}
    		i++;
		}
    	return count;
    }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
